﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
using System.Windows;
using System.Windows.Shapes;

namespace RssBar
{
    internal static class WorkArea
    {
        [DllImport("user32", CharSet = CharSet.Auto)]
        private static extern bool SystemParametersInfo(int uAction,
            int uParam, ref RECT lpvParam, int fuWinIni);

        [StructLayout(LayoutKind.Sequential)]
        public struct RECT
        {
            int _left;
            int _top;
            int _right;
            int _bottom;

            public RECT(Rect rectangle)
                : this((int)rectangle.Left, (int)rectangle.Top, (int)rectangle.Right, (int)rectangle.Bottom)
            {
            }
            public RECT(int left, int top, int right, int bottom)
            {
                _left = left;
                _top = top;
                _right = right;
                _bottom = bottom;
            }

            public int X
            {
                get { return Left; }
                set { Left = value; }
            }
            public int Y
            {
                get { return Top; }
                set { Top = value; }
            }
            public int Left
            {
                get { return _left; }
                set { _left = value; }
            }
            public int Top
            {
                get { return _top; }
                set { _top = value; }
            }
            public int Right
            {
                get { return _right; }
                set { _right = value; }
            }
            public int Bottom
            {
                get { return _bottom; }
                set { _bottom = value; }
            }
            public int Height
            {
                get { return Bottom - Top; }
                set { Bottom = value - Top; }
            }
            public int Width
            {
                get { return Right - Left; }
                set { Right = value + Left; }
            }
            public Point Location
            {
                get { return new Point(Left, Top); }
                set
                {
                    Left = (int)value.X;
                    Top = (int)value.Y;
                }
            }
            public Size Size
            {
                get { return new Size(Width, Height); }
                set
                {
                    Right = (int)value.Width + Left;
                    Bottom = (int)value.Height + Top;
                }
            }

            public Rect ToRectangle()
            {
                return new Rect(this.Left, this.Top, this.Width, this.Height);
            }
            public static Rect ToRectangle(RECT Rectangle)
            {
                return Rectangle.ToRectangle();
            }
            public static RECT FromRectangle(Rect Rectangle)
            {
                return new RECT((int)Rectangle.Left,
                    (int)Rectangle.Top,
                    (int)Rectangle.Right,
                    (int)Rectangle.Bottom);
            }

            public static implicit operator Rect(RECT Rectangle)
            {
                return Rectangle.ToRectangle();
            }
            public static implicit operator RECT(Rect Rectangle)
            {
                return new RECT(Rectangle);
            }
            public static bool operator ==(RECT Rectangle1, RECT Rectangle2)
            {
                return Rectangle1.Equals(Rectangle2);
            }
            public static bool operator !=(RECT Rectangle1, RECT Rectangle2)
            {
                return !Rectangle1.Equals(Rectangle2);
            }

            public override string ToString()
            {
                return "{Left: " + Left + "; " + "Top: " + Top + "; Right: " + Right + "; Bottom: " + Bottom + "}";
            }

            public bool Equals(RECT Rectangle)
            {
                return Rectangle.Left == Left && Rectangle.Top == Top && Rectangle.Right == Right && Rectangle.Bottom == Bottom;
            }
            public override bool Equals(object Object)
            {
                if (Object is RECT)
                {
                    return Equals((RECT)Object);
                }
                else if (Object is Rect)
                {
                    return Equals(new RECT((Rect)Object));
                }

                return false;
            }

            public override int GetHashCode()
            {
                return Left.GetHashCode() ^ Right.GetHashCode() ^ Top.GetHashCode() ^ Bottom.GetHashCode();
            }
        }

        private const int SPI_GETWORKAREA = 0x0030;
        private const int SPI_SETWORKAREA = 0x002F;
        private const int SPIF_SENDCHANGE = 0x02;

        public static RECT AppWorkArea = new RECT();
        public static RECT OldWorkArea = new RECT();
        public static RECT NewWorkArea = new RECT();

        public static bool SetArea(double height, ref RECT workArea)
        {
            bool res = false;
            RECT oldArea = new RECT();

            res = SystemParametersInfo(SPI_GETWORKAREA, 0, ref oldArea, 0);
            if (!res)
            {
                MessageBox.Show("Error: Get work area");
                return false;
            }

            //zapamiętaj rozmiar ekranu
            OldWorkArea = oldArea;

            RECT newArea = new RECT();
            newArea.Top = oldArea.Top + (int)height;
            newArea.Left = oldArea.Left;
            newArea.Bottom = oldArea.Bottom;
            newArea.Right = oldArea.Right;

            res = SystemParametersInfo(SPI_SETWORKAREA, 1, ref newArea, SPIF_SENDCHANGE);
            if (!res)
            {
                return false;
            }

            if (workArea != null)
            {
                workArea = newArea;
            }

            NewWorkArea = newArea;

            return true;
        }
    }
}
